<?php
/**
 * Created by PhpStorm.
 * User: hp
 * Date: 2017/11/21
 * Time: 10:42
 */

namespace App\Modules\Trade\Services;

use DB;
use App\Modules\Trade\Models\Order;

class OrderService
{
    //获取渠道日数据
    static function getChannelDateOrderInfo($distribution_channel_id, $date)
    {
        return Order::getChannelDateOrderInfo($distribution_channel_id, $date);
    }

    /**
     * 获取付费用户数
     * @param $send_order_id
     * @return mixed
     */
    static function getPayUserNum($send_order_id)
    {
        return Order::getPayUserNum($send_order_id);
    }

    //获取派单累计充值（追踪用户）
    static function getPromotionRegUserRechargeAmount($params = [])
    {
        $end_time = isset($params['end_time']) ? $params['end_time'] : date('Y-m-d H:i:s');
        $begin_time = isset($params['begin_time']) ? $params['begin_time'] : date('Y-m-d H:i:s');
        $distribution_channel_id = isset($params['distribution_channel_id']) ? $params['distribution_channel_id'] : 0;
        if ($distribution_channel_id) {
            $sql = "SELECT SUM(price) as amount FROM send_orders JOIN users ON users.send_order_id=send_orders.id
	JOIN orders ON orders.uid=users.id
 WHERE send_orders.distribution_channel_id={$distribution_channel_id}
 AND orders.`status`='PAID' AND send_orders.send_time BETWEEN '{$begin_time}' AND '{$end_time}'
 AND (UNIX_TIMESTAMP(orders.created_at)-UNIX_TIMESTAMP(send_orders.send_time))<=" . ($params['day_count'] * 24 * 3600);
        } else {
            $sql = "SELECT SUM(price) as amount FROM send_orders JOIN users ON users.send_order_id=send_orders.id
	JOIN orders ON orders.uid=users.id
 WHERE orders.`status`='PAID' AND send_orders.send_time BETWEEN '{$begin_time}' AND '{$end_time}'
 AND (UNIX_TIMESTAMP(orders.created_at)-UNIX_TIMESTAMP(send_orders.send_time))<=" . ($params['day_count'] * 24 * 3600);
        }

        $data = DB::select($sql);
        //\Log::info($sql);
        return (float)$data[0]->amount;
    }

    //获取派单累计充值（追踪用户）
    static function getPromotionRegUserRechargeAmountChannelIds($params = [])
    {
        $end_time = isset($params['end_time']) ? $params['end_time'] : '';
        $begin_time = isset($params['begin_time']) ? $params['begin_time'] : '';
        $distribution_channel_ids = isset($params['distribution_channel_ids']) ? $params['distribution_channel_ids'] : 0;

        $distribution_channel_ids = implode(',', $distribution_channel_ids);
        if ($distribution_channel_ids && $begin_time && $end_time) {
            $sql = "SELECT SUM(price) as amount FROM send_orders JOIN users ON users.send_order_id=send_orders.id
	JOIN orders ON orders.uid=users.id
 WHERE send_orders.distribution_channel_id IN (" . $distribution_channel_ids . ")
 AND orders.`status`='PAID' AND send_orders.send_time BETWEEN '{$begin_time}' AND '{$end_time}'
 ";
            if (isset($params['day_count']) && $params['day_count']) {
                $sql = $sql . " AND (UNIX_TIMESTAMP(orders.created_at)-UNIX_TIMESTAMP(send_orders.send_time))<=" . ($params['day_count'] * 86400);
            }
        }
        //\Log::info($sql);
        $data = DB::select($sql);
        return (float)$data[0]->amount;
    }

    //获取派单累计充值（追踪用户）
    static function getMonthRechargeAmountChannelIds($params = [])
    {
        $end_time = isset($params['end_time']) ? $params['end_time'] : '';
        $begin_time = isset($params['begin_time']) ? $params['begin_time'] : '';
        $distribution_channel_ids = isset($params['distribution_channel_ids']) ? $params['distribution_channel_ids'] : 0;

        $distribution_channel_ids = implode(',', $distribution_channel_ids);
        if ($distribution_channel_ids && $begin_time && $end_time) {
            $sql = "SELECT SUM(price) as amount FROM send_orders JOIN orders ON orders.send_order_id=send_orders.id
 WHERE send_orders.distribution_channel_id IN (" . $distribution_channel_ids . ")
 AND orders.`status`='PAID' AND send_orders.send_time BETWEEN '{$begin_time}' AND '{$end_time}'
 ";
            if (isset($params['day_count']) && $params['day_count']) {
                $sql = $sql . " AND (UNIX_TIMESTAMP(orders.created_at)-UNIX_TIMESTAMP(send_orders.send_time))<=" . ($params['day_count'] * 86400);
            }
        }

        $data = DB::select($sql);
        return (float)$data[0]->amount;
    }

    /**
     * 获取派单累计充值（追踪用户）
     * @param array $params
     * @return float
     */
    static function getPromotionRegUserRechargeAmountBySendOrerId($params = [])
    {
        $end_time = isset($params['end_time']) ? $params['end_time'] : '';
        $start_time = isset($params['start_time']) ? $params['start_time'] : '';
        $send_order_id = isset($params['send_order_id']) ? $params['send_order_id'] : 0;
        if ($send_order_id) {
            if ($start_time && $end_time) {
                $data = DB::select("select sum(price) amount from orders where status='PAID' and created_at >= '{$start_time}' and created_at <= '{$end_time}' and uid in (select id from users where send_order_id = '{$send_order_id}' )");
            } else {
                $data = DB::select("select sum(price) amount from orders where status='PAID' and uid in (select id from users where send_order_id = '{$send_order_id}' )");
            }
            return (float)$data[0]->amount;
        }
    }

    static function getAmount($params = [])
    {
        return Order::getAmount($params);
    }
}
